package co.fitstart.entity.product.stat;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.product.Product;


/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "user_product_behavior")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class UserProductBehavior {
    
    @Id
    @GeneratedValue
    private Long id;
    
    private String openid;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Product product;
    
    /** 是否阅读 */
    @Column( name = "is_read")
    private int read;
    
    /** 是否分享 */
    @Column( name = "is_shared")
    private int shared;
    
    /** 是否下单 */
    @Column(name = "is_ordered")
    private int ordered;
    
    /** 创建时间 */
    private Date createTime;
    
    /** 最后分享时间 */
    private Date shareTime;
    
    /** 最后下单时间 */
    private Date orderTime;
    
    /** 阅读来源 */
    private String source;
    
    /** 下单来源 */
    private String orderSource;
    
    /**
     * The default constructor 
     */
    public UserProductBehavior() {
    
    }
    
    /**
     * The constructor using fields
     * 
     * @param product
     * @param openid
     */
    public UserProductBehavior(Product product, String openid) {
        this.product = product;
        this.openid = openid;
        this.read = 1;
        this.createTime = new Date();
    }
    
    /**
     * read product
     * @return
     */
    public UserProductBehavior read() {
        this.read = 1;
        return this;
    }
    
    /**
     * share product
     * @return
     */
    public UserProductBehavior share() {
        this.shared = 1;
        this.shareTime = new Date();
        return this;
    }
    
    /**
     * order product
     * @return
     */
    public UserProductBehavior order() {
        this.ordered = 1;
        this.orderTime = new Date();
        return this;
    }

    /**
     * @return the read
     */
    public boolean isRead() {
        return this.read > 0;
    }

    /**
     * @return the shared
     */
    public boolean isShared() {
        return this.shared > 0;
    }
    
    /**
     * 
     * @return
     */
    public boolean isOrdered() {
        return this.ordered > 0;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the openid
     */
    public String getOpenid() {
        return openid;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }
    
    /**
     * @return the read
     */
    public int getRead() {
        return read;
    }
    
    /**
     * @return the shared
     */
    public int getShared() {
        return shared;
    }

    /**
     * @return the ordered
     */
    public int getOrdered() {
        return ordered;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the shareTime
     */
    public Date getShareTime() {
        return shareTime;
    }

    /**
     * @return the orderTime
     */
    public Date getOrderTime() {
        return orderTime;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @return the orderSource
     */
    public String getOrderSource() {
        return orderSource;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param openid the openid to set
     */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @param read the read to set
     */
    public void setRead(int read) {
        this.read = read;
    }

    /**
     * @param shared the shared to set
     */
    public void setShared(int shared) {
        this.shared = shared;
    }

    /**
     * @param ordered the ordered to set
     */
    public void setOrdered(int ordered) {
        this.ordered = ordered;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param shareTime the shareTime to set
     */
    public void setShareTime(Date shareTime) {
        this.shareTime = shareTime;
    }

    /**
     * @param orderTime the orderTime to set
     */
    public void setOrderTime(Date orderTime) {
        this.orderTime = orderTime;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @param orderSource the orderSource to set
     */
    public void setOrderSource(String orderSource) {
        this.orderSource = orderSource;
    }
    
}
